<?php
function update_title($Q,$dblayer,$params) {

	$titleImageSrc = "";
	//if (isset($Q['icons']['map']['large']['actions']['documentation'])) {
		//$titleImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['actions']['documentation'];
	//} else {
		$titleImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	//}
	$title = "<h1><img valign='middle' src='{$titleImageSrc}' />&nbsp;Update Module</h1>\n";
	
	return $title;
}
function update_schema_check($Q,$dblayer,$schemaFile,$schemaLocation,$update=false) {
	$page = "";
	$sqlupdate = "";//the eventual transaction of SQL statements
	if (file_exists($schemaFile)) {
		$xml = simplexml_load_file($schemaFile);
		if ($xml) {
			foreach ($xml->database as $db) {
				$dbLabel = $db->label;
				$dbConfigFile = $schemaLocation."/db.php";
				if (file_exists($dbConfigFile)) {
					/*
						If the db config file does not exist assume the DB is either SYSTEM or USER
						
						The label from the XML file is always used regardless of whether a dbfile exists.
					*/
					$default->loadDBconfigFile($file);
				}
					foreach ($xml->tables->table as $table) {
						$sql = sprintf("select * from `%s`",$table->name);
						//$sdbname = $default->param($dbLabel,"dbname");
						//$sdbconn = $default->param($dbLabel,"dbconn");
						//mysql_select_db($sdbname, $sdbconn);
						//$result = @mysql_query($sql,$sdbconn);
						$result = @$dblayer->query($dbLabel,$sql);
						if (!$result) {
							$page .= "<p>".$table->name." Does not exist!</p>\n";
						} else { //if not result
							$page .= "<p>".$table->name." Exists</p>\n";
							//now check the fields exist and that they are configured the same.
						}//if not result else
					}//foreach table
			}//foreach database
		}//if xml
	}//if file exists schemaFile
	return $page;
}//function update_schema_check
function update_task($Q,$dblayer,$providers,$params) {
$page = array();
$page['title'] = "";
$page['body'] = "";
	if (isset($params['task'])) {
		$task = $params['task'];
		$modref = "";
		if (isset($params['modref'])) {
			$modref = $params['modref'];
		}
		switch ($task) {
		default:
			/** Specifiy the default action **/
			$page['title'] .= "Update Module";
			$page['body'] .= "<h2>Update Module</h2>\n";
			//run a status report for the database
				//get a list of permitted modules the current user can access
				$userInfo = current_user($dblayer);
				$permitted_modules = permitted_modules($dblayer,$userInfo['id']);
				$modules = $dblayer->query("SYSTEM",sprintf("select * from modules where mid IN %s",array_to_query_list($permitted_modules)));
				if ($modules->num_rows) {
					//see if module has a database schema definition
					while ($m = $modules->fetch_object()) {
						$schemaFile = $Q{'absolute_path'}."/".$m->location."/modules/".$modref."/schema.xml";
						$schemaLocation = $Q{'absolute_path'}."/".$m->location."/modules/".$modref;
						if (file_exists($schemaFile)) {
							//this module has a database schema to manage
							$page .= update_schema_check($Q,$dblayer,$schemaFile,$schemaLocation);
						}
					}// while ($modules->row = mysql_fetch_assoc($modules->query));
				}
		break;
		}//switch task
	}
return $page;
}
function update_cpanel($Q,$dblayer,$params) {
	$modref = "update";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$modImageSrc = "";
	//if (isset($Q['icons']['map']['large']['actions']['documentation'])) {
		//$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['actions']['documentation'];
	//} else {
		$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	//}
	$icons = array();
		$icon = array();
			$icon['label'] = "Update Module";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "General";
		$icons[] = $icon;
	return $icons;
}
?>
